/* eslint-disable no-undef */
// import { fileURLToPath, URL } from 'node:url';
import { resolve } from 'path';
import { defineConfig, loadEnv } from 'vite';
import vue from '@vitejs/plugin-vue';
import Icons from 'unplugin-icons/vite';
import IconsResolver from 'unplugin-icons/resolver';
import vueJsx from '@vitejs/plugin-vue-jsx';
import viteCompression from 'vite-plugin-compression';
import AutoImport from 'unplugin-auto-import/vite';
import Components from 'unplugin-vue-components/vite';
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers';

export default ({ mode }) => {
  // // 获取.env文件里定义的环境变量
  const env = loadEnv(mode, process.cwd());
  return defineConfig({
    base: env.VITE_APP_BASE_URL,
    css: {
      preprocessorOptions: {
        less: {
          javascriptEnabled: true,
          additionalData: `@import "${resolve(__dirname, 'src/assets/style/common.less')}";`
        }
      },
      postcss: {
        // plugins: [
        //   pxtorem({
        //     rootValue({ file }) {
        //       return file.indexOf('vant') !== -1 ? 37.5 : 75;
        //     },
        //     unitPrecision: 4,
        //     propList: ['*'],
        //     replace: env.VITE_APP_ENV === 'production',
        //     minPixelValue: 1
        //   })
        // ]
      }
    },
    resolve: {
      alias: {
        // '@': fileURLToPath(new URL('./src', import.meta.url)),
        '@': resolve(__dirname, './src'),
        '@c': resolve(__dirname, './src/components'),
        '@api': resolve(__dirname, './src/common/interface/api'),
        '@busi': resolve(__dirname, './src/common/business'),
        '@img': resolve(__dirname, './src/assets/images'),
        '@style': resolve(__dirname, './src/assets/style'),
        '@utils': resolve(__dirname, './src/common/utils'),
        '@constant': resolve(__dirname, './src/common/constant')
      }
    },
    server: {
      host: true, // 监听所有地址
      port: 5170, //本地端口号
      proxy: {
        //配置代理服务器
        '/servicePath': {
          target: '',
          changeOrigin: true //允许跨域
        }
      }
    },
    plugins: [
      vue(),
      vueJsx(),
      AutoImport({
        // Auto import functions from Vue, e.g. ref, reactive, toRef...
        // 自动导入 Vue 相关函数，如：ref, reactive, toRef 等
        imports: ['vue'],
        resolvers: [ElementPlusResolver()]
      }),
      Components({
        resolvers: [
          // 自动注册图标组件{prefix}-{collection}-{icon}
          IconsResolver({
            prefix: 'Icon',
            enabledCollections: ['ep']
          }),
          // 自动导入 Element Plus 组件
          ElementPlusResolver()
        ]
      }),
      Icons({
        autoInstall: true
      }),
      viteCompression({ threshold: 10240 })
      // legacyPlugin({ // 兼容低版本浏览器
      //   targets: ['chrome 60'] // 需要兼容的目标列表，可以设置多个
      // })
    ],
    build: {
      outDir: 'dist', // 打包文件的输出目录
      assetsDir: 'assets', // 静态资源的存放目录
      assetsInlineLimit: 4096 // 图片转 base64 编码的阈值
    }
  });
};
